package com.wandian.cqcsk.mvvm.mine.invite.invitereward.mthtickethistory.invitehistorypagefragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.cqcsk.R;
import com.wandian.cqcsk.adapter.mine.FriendsRecycleAdapter;
import com.wandian.cqcsk.adapter.rank.RankingPageRecycleAdapter;
import com.wandian.cqcsk.bean.InviteRecordBean;
import com.wandian.cqcsk.databinding.FragmentInviteHistoryPageBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description InviteHistoryPageFragment
 **/
public class InviteHistoryPageFragment extends BaseMVVMFragment<InviteHistoryPageFragmentViewModel> {

    private FragmentInviteHistoryPageBinding dataBinding;
    private FriendsRecycleAdapter adapter;
    private String friends_json;
    private String title="";

    public static InviteHistoryPageFragment newInstance(String title,String friends_json) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("friends_json", friends_json);
        InviteHistoryPageFragment fragment = new InviteHistoryPageFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = FragmentInviteHistoryPageBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        title = getArguments().getString("title");
        friends_json = getArguments().getString("friends_json");
        dataBinding.setViewModel(mViewModel);
        initRecycler();
    }

    private void  initRecycler(){
        if(!UIUtils.isEmpty(title) && title.trim().equals("我的好友"))
            adapter = new FriendsRecycleAdapter(R.layout.item_friends_style_one,"0");
        else
            adapter = new FriendsRecycleAdapter(R.layout.item_friends_style_one,"1");
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recycle.setLayoutManager(layoutManager);
        dataBinding.recycle.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InviteRecordBean bean = (InviteRecordBean)adapter.getData().get(position);
                if(!UIUtils.isEmpty(bean)){
                    UserInfoBean userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
                    Bundle bundle = new Bundle();
                    bundle.putString("title","动态");
                    bundle.putString("user_id",bean.getId());
                    bundle.putString("user_name",bean.getNickName());
                    bundle.putString("user_head",bean.getHeadPic());
                    bundle.putString("form_id",userInfoBean.getId()+"");
                    bundle.putString("user_des",bean.getDescription()+"");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_USERHEADINFO).with(bundle).navigation();
                }
            }
        });
        if(!UIUtils.isEmpty(friends_json)){
            List<InviteRecordBean> listBean = JsonUtil.fromJson(friends_json, new TypeToken<ArrayList<InviteRecordBean>>() {});
            if(!UIUtils.isEmpty(listBean) && listBean.size()>0){
                adapter.setNewData(listBean);
            }else  adapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, null));
        }else adapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, null));
    }

}
